package com.cxs.controller;

import com.cxs.model.Answers;
import com.cxs.model.Questions;
import com.cxs.service.QuestionService;
import cxs.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/*
 * @Author:cxs
 * @Date:2021/10/3  15:51
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(){
        return Result.OK("请求成功",questionService.list());
    }

    @RequestMapping("/questionDetail")
    public String questionDetail(Integer id, Model model){
        if(id == null){
            model.addAttribute("msg","参数传递异常");
            return "message";
        }

        Questions detail = questionService.detail(id);
        if(detail != null){
            model.addAttribute("detail",detail);
        }
        return "question_detail";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Questions questions, Model model){
        if(questions == null && questions.getTitle() != null && questions.getDefaultDesc() != null){
            model.addAttribute("msg","参数传递异常");
            return "message";
        }

        questionService.add(questions);
        return "index";
    }
}
